package com.aiyi.server.manager.nginx.bean;

import java.io.File;
import java.io.FileFilter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aiyi.server.manager.nginx.bean.result.Result;
import com.aiyi.server.manager.nginx.common.CMDUtil;
import com.aiyi.server.manager.nginx.common.CommonFields;
import com.aiyi.server.manager.nginx.core.thread.SystemProcessManager;

public class ServiceManager {

    private static final Logger logger = LoggerFactory.getLogger(ServiceManager.class);
	
	private String programName;
	
	private String serviceName;
	
	private ServiceManager(String programName) {
		this.programName = programName;
	}

	public static ServiceManager getInstance(String nginxPath) {
		ServiceManager serviceManager = null;
		
		// 如果是 Windows 系统，搜索是否有服务包装程序
		if (CMDUtil.isWindows()) {
			
			File file = new File(nginxPath);
			if(file.exists()&&file.isDirectory()){
				 File[] files = file.listFiles(new FileFilter() {
					
					@Override
					public boolean accept(File fileName) {
						boolean result = false;
						
						String nameString = fileName.getName().toLowerCase();
						// 文件名 nginx 开始，以 .exe 结尾 但不是 nginx.exe 
						if(fileName.isFile() && !nameString.equals("nginx.exe") && nameString.startsWith("nginx") && nameString.endsWith(".exe")) {
							//  有同名 .xml 文件
							File confFile = new File(fileName.getAbsolutePath().replace(".exe", ".xml"));
							if(confFile.exists()) {
							}else {
							}
							result = true;
						}
						return result;
					}
				});
				 
				if(files.length>0) {
					for (File file2 : files) {
						serviceManager = new ServiceManager(file2.getAbsolutePath());
					}
				}
			}
			
		}
		
		return serviceManager;
	}

	public Result stop() {
		Result result = new Result();
		
		File file = new File(programName);
		if(file.getName().equals("nginx_server.exe")) {
			// 标准服务方式停止
			String excuse = CMDUtil.excuse("net.exe stop nginx", null);
			String[] returlStrings = excuse.split("\r\n");
			
			if (returlStrings.length>1) {
				int index = returlStrings[1].indexOf(" FATAL ");
				if(index>=0) {
					result.setMessage(returlStrings[1].substring(index+9));
					result.setSuccess(false);
				}
			}else {
				result.setSuccess(true);
			}
		} else {
			// 专用服务方式停止
			String excuse = CMDUtil.excuse("\""+programName+"\"" + " stop", null);
			String[] returlStrings = excuse.split("\r\n");
			
			if (returlStrings.length>1) {
				int index = returlStrings[1].indexOf(" FATAL ");
				if(index>=0) {
					result.setMessage(returlStrings[1].substring(index+9));
					result.setSuccess(false);
				}
			}else {
				result.setSuccess(true);
			}
		}
		if (!result.isSuccess()) {
			logger.error("Nginx服务停止失败:" + result.getMessage() + ";尝试强制结束");
			SystemProcessManager.kill(CommonFields.NGINX);
			logger.info("Nginx服务已强制结束");
		}
		
		return result;
	}

	public Result start() {
		Result result = new Result();
		
		File file = new File(programName);
		if(file.getName().equals("nginx_server.exe")) {
			// 标准服务启动方式
			String excuse = CMDUtil.excuse("net.exe start nginx", null);
			String[] returlStrings = excuse.split("\r\n");
			
			if (returlStrings.length>1) {
				int index = returlStrings[1].indexOf(" FATAL ");
				if(index>=0) {
					result.setMessage(returlStrings[1].substring(index+9));
					result.setSuccess(false);
				}
			}else {
				result.setSuccess(true);
			}
		} else {
			// 专用服务启动方式
			String excuse = CMDUtil.excuse("\""+programName+"\"" + " start", null);
			String[] returlStrings = excuse.split("\r\n");
			
			if (returlStrings.length>1) {
				int index = returlStrings[1].indexOf(" FATAL ");
				if(index>=0) {
					result.setMessage(returlStrings[1].substring(index+9));
					result.setSuccess(false);
				}
			}else {
				result.setSuccess(true);
			}
		}
		if (!result.isSuccess()) {
		}
		
		return result;
	}

}
